//
//  LXSearchTableViewCell.m
//  cat
//
//  Created by imera on 16/1/20.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXSearchTableViewCell.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

#define LXSearchTableViewCellMargin 10

@interface LXSearchTableViewCell()

@property (nonatomic, weak) UIImageView *videoImageView;

@property (nonatomic, weak) UILabel *titleLabel;

@property (nonatomic, weak) UIImageView *playIcon;

@property (nonatomic, weak) UILabel *playNumLabel;

@property (nonatomic, weak) UIImageView *commentIcon;

@property (nonatomic, weak) UILabel *commentLabel;

@end

@implementation LXSearchTableViewCell

- (void)setVideo:(LXVideo *)video
{
    if (video != nil) {
        _video = video;
    }
    
    [self layoutSubviews];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
        self.backgroundColor = MainBackgroundColor;
    }
    return self;
}

- (void)initSubViews
{
    UIImageView *videoImageView = [[UIImageView alloc]init];
    self.videoImageView = videoImageView;
    [self addSubview:videoImageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.numberOfLines = 2;
    titleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
    
    UIImageView *playIcon = [[UIImageView alloc]init];
    playIcon.image = [UIImage imageNamed:@"eye"];
    self.playIcon = playIcon;
    [self addSubview:playIcon];
    
    UILabel *playNumLabel = [[UILabel alloc]init];
    playNumLabel.font = [UIFont systemFontOfSize:13];
    playNumLabel.textColor = [UIColor grayColor];
    self.playNumLabel = playNumLabel;
    [self addSubview:playNumLabel];
    
    UIImageView *commentIcon = [[UIImageView alloc]init];
    commentIcon.image = [UIImage imageNamed:@"danmu"];
    self.commentIcon = commentIcon;
    [self addSubview:commentIcon];
    
    UILabel *commentLabel = [[UILabel alloc]init];
    commentLabel.font = [UIFont systemFontOfSize:13];
    commentLabel.textColor = [UIColor grayColor];
    self.commentLabel = commentLabel;
    [self addSubview:commentLabel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UIImageView *videoImageView = self.videoImageView;
    [videoImageView sd_setImageWithURL:[NSURL URLWithString:self.video.thumb]];
    CGFloat videoImageViewX = LXSearchTableViewCellMargin;
    CGFloat videoImageViewH = self.frame.size.height - 10;
    CGFloat videoImageViewY = (self.frame.size.height - videoImageViewH) / 2;
    CGFloat videoImageViewW = 100;
    videoImageView.frame = CGRectMake(videoImageViewX, videoImageViewY, videoImageViewW, videoImageViewH);
    
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.video.fname;
    CGFloat titleLabelX = CGRectGetMaxX(videoImageView.frame) + LXSearchTableViewCellMargin;
    CGFloat titleLabelY = videoImageViewY;
    CGFloat titleLabelW = self.frame.size.width - videoImageViewW - LXSearchTableViewCellMargin * 3;
    CGFloat titleLabelH = 40;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    UIImageView *playIcon = self.playIcon;
    CGFloat platIconX = titleLabelX + 5;
    CGFloat platIconY = CGRectGetMaxY(titleLabel.frame) + LXSearchTableViewCellMargin;
    CGFloat platIconW = 15;
    CGFloat platIconH = platIconW;
    playIcon.frame = CGRectMake(platIconX, platIconY, platIconW, platIconH);
    
    UILabel *playNumLabel = self.playNumLabel;
    playNumLabel.text = [NSString stringWithFormat:@"%ld",self.video.viewcounts];
    CGSize playNumLabelSize = [playNumLabel.text sizeWithAttributes:@{NSFontAttributeName:playNumLabel.font}];
    CGFloat plaplayNumLabelX = CGRectGetMaxX(playIcon.frame) + 3;
    CGFloat plaplayNumLabelY = platIconY;
    CGFloat plaplayNumLabelW = playNumLabelSize.width;
    CGFloat plaplayNumLabelH = platIconW;
    playNumLabel.frame = CGRectMake(plaplayNumLabelX, plaplayNumLabelY, plaplayNumLabelW, plaplayNumLabelH);
    
    UIImageView *commentIcon = self.commentIcon;
    CGFloat commentIconX = CGRectGetMaxX(playNumLabel.frame) + 3;
    CGFloat commentIconY = platIconY;
    CGFloat commentIconW = platIconW;
    CGFloat commentIconH = platIconW;
    commentIcon.frame = CGRectMake(commentIconX, commentIconY, commentIconW, commentIconH);
    
    UILabel *commentLabel = self.commentLabel;
    commentLabel.text = [NSString stringWithFormat:@"%ld",self.video.videocomments];
    CGSize commentLabelSize = [commentLabel.text sizeWithAttributes:@{NSFontAttributeName:commentLabel.font}];
    CGFloat commentLabelX = CGRectGetMaxX(commentIcon.frame) + 3;
    CGFloat commentLabelY = platIconY;
    CGFloat commentLabelW = commentLabelSize.width;
    CGFloat commentLabelH = platIconW;
    commentLabel.frame = CGRectMake(commentLabelX, commentLabelY, commentLabelW, commentLabelH);
}

@end
